#include <climits>
#include <string>
#include <vector>
#include <utility>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};


// 814. 二叉树剪枝 (深搜 剪枝)
// https://leetcode.cn/problems/binary-tree-pruning/description/
class Solution {
public:
    TreeNode* pruneTree(TreeNode* root) {
        if (root == nullptr) return nullptr;

        root->left = pruneTree(root->left);
        root->right = pruneTree(root->right);

        if (!root->val && !root->left && !root->right)
        {
            delete root;
            root = nullptr;
        }
        return root;
    }
};

///////////////////////////////////////////////////////////////////////////////
// 230. 二叉搜索树中第K小的元素 (深搜)
// https://leetcode.cn/problems/kth-smallest-element-in-a-bst/description/
class Solution {
public:
    void dfs(TreeNode* root, int& cnt, int* out)
    {
        if (root == nullptr) return;
        
        dfs(root->left, cnt, out);
        --cnt;
        if (cnt == 0)
        {
            *out = root->val;
            return;
        }
        dfs(root->right, cnt, out);
    }

    int kthSmallest(TreeNode* root, int k) {
        int ans = 0;
        dfs(root, k, &ans);
        return ans;
    }
};

///////////////////////////////////////////////////////////////////////////////
// 98. 验证二叉搜索树 (深搜 剪枝)
// https://leetcode.cn/problems/validate-binary-search-tree/description/
class Solution {

    long prevnum = LONG_MIN;

public:
    bool isValidBST(TreeNode* root) {
        if (root == nullptr) return true;
        bool cur = false;
        bool left = isValidBST(root->left);

        if (!left) return false;
        if (root->val > prevnum)
            cur = true;
        if (!cur)
            return false;
        prevnum = root->val;

        bool right = isValidBST(root->right);
        return cur && left && right;
    }
};

///////////////////////////////////////////////////////////////////////////////
// 257. 二叉树的所有路径 (深搜 剪枝)
// https://leetcode.cn/problems/binary-tree-paths/description/
class Solution {  
public:
    void dfs(TreeNode* root, string str, vector<string>& ans)
    {
        str += to_string(root->val);
        if (root->left==nullptr && root->right==nullptr)
        {
            ans.push_back(move(str));
            return;
        }
        
        str += "->";
        if (root->left) dfs(root->left, str, ans);
        if (root->right) dfs(root->right, str, ans);
    }

    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> ans;
        dfs(root, "", ans);
        return ans;
    }
};

///////////////////////////////////////////////////////////////////////////////
// 46. 全排列 (深搜  回溯 剪枝)
// https://leetcode.cn/problems/permutations/description/
class Solution {

    vector<vector<int>> ans;
    vector<int> path;
    bool check[7];

public:
    vector<vector<int>> permute(vector<int>& nums) {
        dfs(nums);
        return ans;
    }

    void dfs(vector<int>& nums)
    {
        if (path.size() == nums.size())
        {
            ans.push_back(path);
            return ;
        }
        
        for (int i = 0; i<nums.size(); ++i)
        {
            if (!check[i]) //剪枝
            {
                path.push_back(nums[i]);
                check[i] = true;
                dfs(nums);

                // 回溯
                path.pop_back();
                check[i] = false;
            }
        }
    }
};